package com.chenl.interview.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.chenl.interview.common.BaseResponse;
import com.chenl.interview.common.ErrorCode;
import com.chenl.interview.common.ResultUtils;
import com.chenl.interview.common.ThrowUtils;
import com.chenl.interview.constant.UserConstant;
import com.chenl.interview.exception.BusinessException;
import com.chenl.interview.model.dto.questionBankQuestion.QBQAddRequest;
import com.chenl.interview.model.dto.questionBankQuestion.QBQBatchAddRequest;
import com.chenl.interview.model.dto.questionBankQuestion.QBQDeleteRequest;
import com.chenl.interview.service.QuestionBankQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 题库题目关联接口
 * @author chenl
 * @version 1.0.0
 */

@RestController
@RequestMapping("/questionBankQuestion")
@Api
@Slf4j
public class QuestionBankQuestionController {
    @Autowired
    private QuestionBankQuestionService questionBankQuestionService;

    @PostMapping("/add")
    @SaCheckRole(value = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "添加题库题目关联")
    public BaseResponse<Boolean> addQBQRelevance(@RequestBody QBQAddRequest qbqAddRequest) {
        ThrowUtils.throwIf(qbqAddRequest == null, ErrorCode.PARAMS_ERROR);

        Long questionId = qbqAddRequest.getQuestionId();
        Long questionBankId = qbqAddRequest.getQuestionBankId();
        Boolean result = questionBankQuestionService.addQBQRelevance(questionId,questionBankId);
        if (!result){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    // TODO: 2025/1/3 删除题目题库关联
    @PostMapping("/delete")
    @SaCheckRole(value = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除题目题库关联")
    public BaseResponse<Boolean> deleteQBQRelevance(@RequestBody QBQDeleteRequest qbqDeleteRequest){
        ThrowUtils.throwIf(qbqDeleteRequest == null,ErrorCode.PARAMS_ERROR);
        Long questionId = qbqDeleteRequest.getQuestionId();
        Long questionBankId = qbqDeleteRequest.getQuestionBankId();
        Boolean reslurt = questionBankQuestionService.deleteQBQRelevance(questionId,questionBankId);
        if (!reslurt) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }

        return ResultUtils.success(reslurt);
    }

    // TODO: 2025/1/3 向题库批量插入题目
    @PostMapping("/add/batch")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    @ApiOperation("批量向题库中插入题目")
    public BaseResponse<Boolean> batchAddQuestionsToBank(@RequestBody QBQBatchAddRequest qbqBatchAddRequest) {
        // 参数校验
        ThrowUtils.throwIf(qbqBatchAddRequest == null, ErrorCode.PARAMS_ERROR);
        Long questionBankId = qbqBatchAddRequest.getQuestionBankId();
        List<Long> questionIdList = qbqBatchAddRequest.getQuestionIdList();
        questionBankQuestionService.batchAddQuestionsToBank(questionIdList, questionBankId);
        return ResultUtils.success(true);
    }

    // TODO: 2025/1/3 向题库批量删除题目

}
